package com.example.dell.library.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dell.library.R;
import com.example.dell.library.adapter.BookBorrorAdapter;
import com.example.dell.library.bean.BorrorBook;
import com.example.dell.library.bean.BorrorBookList;
import com.example.dell.library.bean.StaticStuBBook;
import com.example.dell.library.bean.Student;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

import static java.lang.Integer.parseInt;


public class ManagerFragment extends Fragment{
    private RecyclerView recyclerView;
    private BookBorrorAdapter borrorAdapter;
    private List<BorrorBookList> BList = new ArrayList<>();
    private String BookObj;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_manager, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = getActivity().findViewById(R.id.rv_Manger);
        chaxun();

    }

    private void chaxun() {
        BmobQuery<BorrorBook> query = new BmobQuery<BorrorBook>();
        query.addWhereEqualTo("SID", StaticStuBBook.bid);
        query.findObjects(new FindListener<BorrorBook>() {
            @Override
            public void done(List<BorrorBook> list, BmobException e) {
                if (list.size()!=0){
                    BorrorBook borrorBook = list.get(0);
                    BookObj = borrorBook.getObjectId();
                    if (borrorBook.getSBook1() != "" && borrorBook.getSBook1() != null) {
                        Log.e("111111", BookObj + "*");
                        BorrorBookList bl = new BorrorBookList(BookObj, borrorBook.getSBook1(), borrorBook.getTime1(), dtime(borrorBook.getTime1()),1111);
                        BList.add(bl);
                    }
                    if (borrorBook.getSBook2() != "" && borrorBook.getSBook2() != null) {
                        Log.e("222222", borrorBook.getTime2());
                        BorrorBookList b2 = new BorrorBookList(BookObj, borrorBook.getSBook2(), borrorBook.getTime2(), dtime(borrorBook.getTime2()),2222);
                        BList.add(b2);
                    }
                    if (borrorBook.getSBook3() != "" && borrorBook.getSBook3() != null) {
                        Log.e("333333", borrorBook.getTime3());
                        BorrorBookList b3 = new BorrorBookList(BookObj, borrorBook.getSBook3(), borrorBook.getTime3(), dtime(borrorBook.getTime3()),3333);
                        BList.add(b3);
                    }
                }else {
                    String StuJson = getActivity().getIntent().getStringExtra("STUDENT");
                    Student sss = new Gson().fromJson(StuJson, Student.class);
                    BorrorBook gameScore = new BorrorBook();
                    gameScore.setSID(sss.getObjectId());
                    gameScore.setSNO(sss.getStuNo());
                    gameScore.save(new SaveListener<String>() {
                        @Override
                        public void done(String objectId, BmobException e) {
                            if(e==null){
                                Log.e("xxxxxxxxxxxx","xx：");
                            }else{
                                Log.e("yyyyyyyyyyyy","xx：");
                            }
                        }
                    });
                }

                LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                borrorAdapter = new BookBorrorAdapter(BList);
                recyclerView.setAdapter(borrorAdapter);
            }
        });
    }

    private String dtime(String time3) {
        int day = parseInt(time3.substring(8, 10));
        int month = parseInt(time3.substring(5, 7))+1;
        int year = parseInt(time3.substring(0, 4));
        String time = time3.substring(11, 19);
        if (month > 12) {
            month = month - 12;
            year = year + 1;
        }
        if (month<10){
            return year + "-0" + month + "-" + day + " " + time;
        }
        Log.e("hh22", year + "-" + month + "-" + day + " " + time);
        return year + "-" + month + "-" + day + " " + time;
    }
}
